package com.borya.model.entity;

import java.math.BigDecimal;

import com.borya.model.db.MerchantLevelConfig;

public class MerchantLevelInfo {
	//商户等级相关信息
	private String dealerId;//商户号
	private Integer successNums;//开卡成功次数
	private Integer validNums;//有效开卡次数
	
	private Integer getPoints;//获得积分，1000倍
	private Integer losePoints;//扣除积分，1000倍
	
	private Integer creditPoints;//信用积分,加上保证金，1000倍
	private Integer basePoints=950;//信用积分标准
	private Integer realCreditPoints;//真实信用积分，排除保证金的干扰，1000倍
	
	private Integer baseNums1=30;//开卡成功数标准1
	private Integer baseNums2=50;//开卡成功数标准2
	
	private Integer merchantExtraCredit;//商户保证金所有积分，1000倍
	private Integer merchantExtraFrequency;//商户保证金所有成功次数，1000倍
	
	private Integer needSuccessNums;//升到下一级所需次数
	private Integer nextLevel;//下一个等级
	private Integer allowUpd=1;  //是否允许升级，1允许0不允许
	private Integer merchantLevel;//商户目前等级
	private Integer realMerchantLevel;//商户真实等级
	
	private Integer forwardLevel;//期望等级
	private Integer getBondNeedNums;//可以退保证金的成功次数
	private Integer base_success_frequency;//基础成功次数
	private Integer base_total_frequency;//基础总次数
	private Integer base_total_score;//基础总分数
	public Integer getSuccessNums() {
		return successNums;
	}
	public void setSuccessNums(Integer successNums) {
		this.successNums = successNums;
	}
	public Integer getValidNums() {
		return validNums;
	}
	public void setValidNums(Integer validNums) {
		this.validNums = validNums;
	}
	public Integer getGetPoints() {
		return getPoints;
	}
	public void setGetPoints(Integer getPoints) {
		this.getPoints = getPoints;
	}
	public Integer getLosePoints() {
		return losePoints;
	}
	public void setLosePoints(Integer losePoints) {
		this.losePoints = losePoints;
	}
	
	public String getDealerId() {
		return dealerId;
	}
	public void setDealerId(String dealerId) {
		this.dealerId = dealerId;
	}
	public Integer getCreditPoints() {
		return creditPoints;
	}
	/**
	 * 当前信用积分，加上保证金的
	 */
	public void setCreditPoints() {
		
		if(this.validNums + this.merchantExtraFrequency + this.base_total_frequency == 0){
			this.creditPoints = 0;
		}else{
			BigDecimal credit = new BigDecimal(this.getPoints).add(new BigDecimal(this.losePoints))   //订单表获取分数
					.add(new BigDecimal(this.merchantExtraCredit))                                    //保证金额外分数
					.add(new BigDecimal(this.merchantExtraFrequency*1000))                            //保证金成功次数额外分数
					.add(new BigDecimal(this.base_total_score))                                       //基础总分数
					.divide(                                                                          //除以
							new BigDecimal(this.validNums)                                            //订单表有效总次数
							.add(new BigDecimal(this.merchantExtraFrequency)                          //保证金成功额外次数
							.add(new BigDecimal(this.base_total_frequency))                           //基础总次数
							),0, BigDecimal.ROUND_DOWN);
			this.creditPoints = credit.intValue();
		}
				
	}
	public Integer getBasePoints() {
		return basePoints;
	}
	public void setBasePoints(Integer basePoints) {
		this.basePoints = basePoints;
	}
	public Integer getBaseNums1() {
		return baseNums1;
	}
	public void setBaseNums1(Integer baseNums1) {
		this.baseNums1 = baseNums1;
	}
	public Integer getBaseNums2() {
		return baseNums2;
	}
	public void setBaseNums2(Integer baseNums2) {
		this.baseNums2 = baseNums2;
	}
	
	public Integer getMerchantExtraCredit() {
		return merchantExtraCredit;
	}
	public void setMerchantExtraCredit(Integer merchantExtraCredit) {
		this.merchantExtraCredit = merchantExtraCredit;
	}
	public Integer getMerchantExtraFrequency() {
		return merchantExtraFrequency;
	}
	public void setMerchantExtraFrequency(Integer merchantExtraFrequency) {
		this.merchantExtraFrequency = merchantExtraFrequency;
	}
	
	
	public Integer getRealCreditPoints() {
		return realCreditPoints;
	}
	/**
	 * 真实信用积分
	 */
	public void setRealCreditPoints() {

		if(this.validNums + this.base_total_frequency ==0){
			this.realCreditPoints = 0;
		}else{
			BigDecimal credit = new BigDecimal(this.getPoints).add(new BigDecimal(this.losePoints))
								.add(new BigDecimal(this.base_total_score))
								.divide(
										new BigDecimal(this.validNums)
										.add(new BigDecimal(this.base_total_frequency)),0, BigDecimal.ROUND_DOWN);
			this.realCreditPoints = credit.intValue();
		}
				
	}
	public Integer getNeedSuccessNums() {
		return needSuccessNums;
	}
	/**
	 * 查询下个等级  与  所需开卡次数
	 * @param credit
	 * @return
	 */
	public void setNeedSuccessNums() {
		if(this.validNums + this.base_total_frequency == 0){
			this.needSuccessNums = this.baseNums1;
			this.nextLevel = 2;
			return;
		}
		if(this.creditPoints >= this.basePoints){
			if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency ) < this.baseNums1){
				this.needSuccessNums = this.baseNums1-
						( this.successNums + this.merchantExtraFrequency + this.base_success_frequency );
				this.nextLevel = 2;
			}else if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency )  >= this.baseNums1
					&& ( this.successNums + this.merchantExtraFrequency + this.base_success_frequency ) < this.baseNums2){
				this.needSuccessNums = this.baseNums2-
						( this.successNums + this.merchantExtraFrequency + this.base_success_frequency );
				this.nextLevel = 3;
			}else{
				//已是vip
				this.needSuccessNums = 0;
				this.nextLevel = 3;
				this.allowUpd = 0;
			}
		}else{
			Integer needNums = new BigDecimal(this.basePoints)
						.multiply(
							new BigDecimal(this.validNums)
							.add(new BigDecimal(this.merchantExtraFrequency))
							.add(new BigDecimal(this.base_total_frequency))
							)
						.subtract(new BigDecimal(this.getPoints))
						.subtract(new BigDecimal(this.losePoints))
						.subtract(
								new BigDecimal(this.merchantExtraFrequency).multiply(new BigDecimal(1000)))
						.subtract(new BigDecimal(this.merchantExtraCredit))
						.subtract(new BigDecimal(this.base_total_score))
						.divide(
								new BigDecimal(1000-this.basePoints),
								0, BigDecimal.ROUND_UP).intValue();
			if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency ) < this.baseNums1){
				if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency )
						+needNums < this.baseNums1){
					this.needSuccessNums = this.baseNums1-
							( this.successNums + this.merchantExtraFrequency + this.base_success_frequency );
					this.nextLevel = 2;
				}else if(
						( this.successNums + this.merchantExtraFrequency + this.base_success_frequency )
						+ needNums >= this.baseNums1 
						&& ( this.successNums + this.merchantExtraFrequency + this.base_success_frequency )
						+ needNums < this.baseNums2 ){
					this.needSuccessNums = needNums;
					this.nextLevel = 2;
				}else{
					this.needSuccessNums = needNums;
					this.nextLevel = 3;
				}
			}else if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency ) >= this.baseNums1){
				if(( this.successNums + this.merchantExtraFrequency + this.base_success_frequency )
						+ needNums < this.baseNums2){
					this.needSuccessNums = needNums;
					this.nextLevel = 2;
				}else{
					this.needSuccessNums = needNums;
					this.nextLevel = 3;
				}
			}
			
		}
	}
	
	public Integer getNextLevel() {
		return nextLevel;
	}
	public void setNextLevel(Integer nextLevel) {
		this.nextLevel = nextLevel;
	}
	public Integer getAllowUpd() {
		return allowUpd;
	}
	public void setAllowUpd(Integer allowUpd) {
		this.allowUpd = allowUpd;
	}
	//将等级翻译成对应汉字
	public String transLevelToName(Integer level, MerchantLevelConfig m){
		if(level==m.getONE_LEVEL()){
			return m.getONE_NAME();
		}else if(level==m.getTWO_LEVEL()){
			return m.getTWO_NAME();
		}else if(level==m.getTHREE_LEVEL()){
			return m.getTHREE_NAME();
		}else{
			return "未知等级";
		}
	}

	//将等级翻译成对应汉字
	public String getPower(Integer level, MerchantLevelConfig m){
		if(level==m.getONE_LEVEL()){
			return m.getONE_POWER_DESC();
		}else if(level==m.getTWO_LEVEL()){
			return m.getTWO_POWER_DESC();
		}else if(level==m.getTHREE_LEVEL()){
			return m.getTHREE_POWER_DESC();
		}else{
			return "未知等级";
		}
	}
	
	public Integer getMerchantLevel() {
		return merchantLevel;
	}
	/**
	 * 当前等级，有保证金
	 */
	public void setMerchantLevel() {
		if((this.successNums + this.merchantExtraFrequency + this.base_success_frequency) < this.baseNums1){
			this.merchantLevel = 1;
		}else if(
				(this.successNums + this.merchantExtraFrequency + this.base_success_frequency) >= this.baseNums1 
				&& (this.successNums + this.merchantExtraFrequency + this.base_success_frequency) < this.baseNums2){
			if(this.creditPoints<this.basePoints){
				this.merchantLevel = 1;
			}else{
				this.merchantLevel = 2;
			}
		}else{
			if(this.creditPoints<this.basePoints){
				this.merchantLevel = 1;
			}else{
				this.merchantLevel = 3;
			}
		}
	}
	public Integer getRealMerchantLevel() {
		return realMerchantLevel;
	}
	public void setRealMerchantLevel() {
		if(( this.successNums + this.base_success_frequency ) < this.baseNums1){
			this.realMerchantLevel = 1;
		}else if(( this.successNums + this.base_success_frequency )  >= this.baseNums1 
				&& ( this.successNums + this.base_success_frequency ) < this.baseNums2){
			if(this.realCreditPoints<this.basePoints){
				this.realMerchantLevel = 1;
			}else{
				this.realMerchantLevel = 2;
			}
		}else{
			if(this.realCreditPoints<this.basePoints){
				this.realMerchantLevel = 1;
			}else{
				this.realMerchantLevel = 3;
			}
		}
	}
	public Integer getBase_success_frequency() {
		return base_success_frequency;
	}
	public void setBase_success_frequency(Integer base_success_frequency) {
		this.base_success_frequency = base_success_frequency;
	}
	public Integer getBase_total_frequency() {
		return base_total_frequency;
	}
	public void setBase_total_frequency(Integer base_total_frequency) {
		this.base_total_frequency = base_total_frequency;
	}
	public Integer getBase_total_score() {
		return base_total_score;
	}
	public void setBase_total_score(Integer base_total_score) {
		this.base_total_score = base_total_score;
	}
	public Integer getForwardLevel() {
		return forwardLevel;
	}
	public void setForwardLevel(Integer forwardLevel) {
		this.forwardLevel = forwardLevel;
	}
	public Integer getGetBondNeedNums() {
		return getBondNeedNums;
	}
	public void setGetBondNeedNums() {
		if(this.realMerchantLevel<this.forwardLevel){
			if( this.validNums + this.base_total_frequency == 0){
				if(this.forwardLevel==2){
					this.getBondNeedNums = this.baseNums1;
				}else{
					this.getBondNeedNums = this.baseNums2;
				}
				return;
			}
			if(this.realCreditPoints>=this.basePoints){
				if((this.successNums+this.base_success_frequency) < this.baseNums1){
					if(this.forwardLevel==2){
						this.getBondNeedNums = this.baseNums1-(this.successNums+this.base_success_frequency);
					}else{
						this.getBondNeedNums = this.baseNums2-(this.successNums+this.base_success_frequency);
					}
				}else if((this.successNums+this.base_success_frequency)  >= this.baseNums1 
						&& (this.successNums+this.base_success_frequency) < this.baseNums2){
					this.getBondNeedNums = this.baseNums2-(this.successNums+this.base_success_frequency);
				}else{
					//已是vip
					this.getBondNeedNums = 0;
				}
			}else{
				Integer needNums = new BigDecimal(this.basePoints)
							.multiply(new BigDecimal(this.validNums+this.base_total_frequency))
							.subtract(new BigDecimal(this.getPoints))
							.subtract(new BigDecimal(this.losePoints))
							.subtract(new BigDecimal(this.base_total_score))
							.divide(new BigDecimal(1000-this.basePoints),
									0, BigDecimal.ROUND_UP).intValue();
				if((this.successNums+this.base_success_frequency) < this.baseNums1){
					if((this.successNums+this.base_success_frequency)+needNums < this.baseNums1){
						if(this.forwardLevel==2){
							this.getBondNeedNums = this.baseNums1-(this.successNums+this.base_success_frequency);
						}else{
							this.getBondNeedNums = this.baseNums2-(this.successNums+this.base_success_frequency);
						}
					}else if((this.successNums+this.base_success_frequency)+needNums  >= this.baseNums1
							&& (this.successNums+this.base_success_frequency)+needNums < this.baseNums2 ){
						if(this.forwardLevel==2){
							this.getBondNeedNums = needNums;
						}else{
							this.getBondNeedNums = this.baseNums2-(this.successNums+this.base_success_frequency);
						}
					}else{
						this.getBondNeedNums = needNums;
					}
				}else if((this.successNums+this.base_success_frequency) >= this.baseNums1){
					if((this.successNums+this.base_success_frequency)+needNums < this.baseNums2){
						this.getBondNeedNums = this.baseNums2-(this.successNums+this.base_success_frequency);
					}else{
						this.getBondNeedNums = needNums;
					}
				}
			}
		}else{
			this.getBondNeedNums = 0;
		}
		
	}
}
